package com.dubbo;

import java.util.*;
import java.util.stream.Collectors;

public class Solution {
    public static void main(String[] args) {
//        List<Map<String,String>> userList=new ArrayList<Map<String,String>>();
//        Map<String, String> map1 = new HashMap<>();
//        map1.put("a","2023-06-14");
//        map1.put("b","10:17:00");
//        Map<String, String> map2 = new HashMap<>();
//        map2.put("a","2023-06-13");
//        map2.put("b","10:17:00");
//        Map<String, String> map3 = new HashMap<>();
//        map3.put("a","2023-06-12");
//        map3.put("b","10:17:00");
//        Map<String, String> map4 = new HashMap<>();
//        map4.put("a","2023-06-11");
//        map4.put("b","10:17:00");
//        userList.add(map1);
//        userList.add(map2);
//        userList.add(map3);
//        userList.add(map4);
//        Map<String, String> map5 = new HashMap<>();
//        map5.put("a","2023-06-15");
//        map5.put("b","10:17:10");
//        Map<String, String> map6 = new HashMap<>();
//        map6.put("a","2023-06-15");
//        map6.put("b","10:17:11");
//        Map<String, String> map7 = new HashMap<>();
//        map7.put("a","2023-06-15");
//        map7.put("b","10:17:12");
//        userList.add(map5);
//        userList.add(map6);
//        userList.add(map7);
//        userList = userList.stream().
//                sorted(
//                        Comparator.comparing(Solution::comparingByA)
//                                .thenComparing(Solution::comparingByB)).collect(Collectors.toList());
//        for (Map<String, String> s : userList) {
//            System.out.println(s);
//        }
//        System.out.println("=====");
//        for (Map<String, String> data : userList.subList(userList.size() - 3, userList.size() )) {
//            System.out.println(data);
//        }
////        List<String> codeList = userList.stream().map(m->m.get("a")).collect(Collectors.toList());
////        for (String s : codeList) {
////            System.out.println(s);
////        }
////        System.out.println(codeList.contains("a1"));
        List<String> codeList = new ArrayList<String>();
        codeList.add("15354067010");
        codeList.add("15354068105");
        codeList.add("18610396380");
        for (String s : codeList) {
            System.out.println(s);
        }
        codeList.remove("15354067010");
        System.out.println(codeList.size());
        System.out.println("======");
        for (String s : codeList) {
            System.out.println(s);
        }
           }

    private static String comparingByA(Map<String, String> map){
        return map.get("a");
    }
    private  static String comparingByB(Map<String, String> map){
        return  map.get("b");
    }

        public List<List<String>> accountsMerge(List<List<String>> accounts) {
        Map<String, Integer> emailToIndex = new HashMap<String, Integer>();
        Map<String, String> emailToName = new HashMap<String, String>();
        int emailsCount = 0;
        for (List<String> account : accounts) {
            String name = account.get(0);
            int size = account.size();
            for (int i = 1; i < size; i++) {
                String email = account.get(i);
                if (!emailToIndex.containsKey(email)) {
                    emailToIndex.put(email, emailsCount++);
                    emailToName.put(email, name);
                }
            }
        }
        UnionFind uf = new UnionFind(emailsCount);
        for (List<String> account : accounts) {
            String firstEmail = account.get(1);
            int firstIndex = emailToIndex.get(firstEmail);
            int size = account.size();
            for (int i = 2; i < size; i++) {
                String nextEmail = account.get(i);
                int nextIndex = emailToIndex.get(nextEmail);
                uf.union(firstIndex, nextIndex);
            }
        }
        Map<Integer, List<String>> indexToEmails = new HashMap<Integer, List<String>>();
        for (String email : emailToIndex.keySet()) {
            int index = uf.find(emailToIndex.get(email));
            List<String> account = indexToEmails.getOrDefault(index, new ArrayList<String>());
            account.add(email);
            indexToEmails.put(index, account);
        }
        List<List<String>> merged = new ArrayList<List<String>>();
        for (List<String> emails : indexToEmails.values()) {
            Collections.sort(emails);
            String name = emailToName.get(emails.get(0));
            List<String> account = new ArrayList<String>();
            account.add(name);
            account.addAll(emails);
            merged.add(account);
        }
        return merged;
    }
}

class UnionFind {
    int[] parent;

    public UnionFind(int n) {
        parent = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }

    public void union(int index1, int index2) {
        parent[find(index2)] = find(index1);
    }

    public int find(int index) {
        if (parent[index] != index) {
            parent[index] = find(parent[index]);
        }
        return parent[index];
    }


}
